<template>
  <view class="container">
    <view class="title">{{ detail.title }}</view>
    <view class="meta">
      <text>发布时间: {{ detail.create_time }}</text>
      <text>热度: {{ detail.hot }}</text>
    </view>
    <view class="content">{{ detail.content }}</view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
const detail = ref({
  title: '',
  content: '',
  create_time: '',
  hot: 0
})

const getNewsDetail = (nid) => {
  uni.request({
    url: 'http://localhost:3010/news/detail',
    method: 'POST',
    header: {
      Authorization: `Bearer ${uni.getStorageSync("token")}`,
      "Content-Type": "application/x-www-form-urlencoded"
    },
    data: {
      nid: nid
    },
    success: (res) => {
      if (res.data.code === 200) {  // 修正判断条件
        detail.value = res.data.data
      } else {
        uni.showToast({
          title: '获取详情失败',
          icon: 'error'
        })
      }
    },
    fail: (err) => {
      console.error('请求失败', err)
      uni.showToast({
        title: '网络错误',
        icon: 'none'
      })
    }
  })
}

import { onLoad } from '@dcloudio/uni-app'  // 使用 uni-app 的 onLoad 代替 vue-router

onLoad((options) => {
  const nid = options.id  // 获取页面参数
  if (nid) {
    getNewsDetail(nid)
  } else {
    uni.showToast({
      title: '缺少新闻ID',
      icon: 'error'
    })
    uni.navigateBack()
  }
})
</script>

<style>
.container {
  padding: 20rpx;
}
.title {
  font-size: 40rpx;
  font-weight: bold;
  margin-bottom: 20rpx;
  color: #333;
}
.content {
  font-size: 32rpx;
  line-height: 1.6;
  color: #666;
  margin-bottom: 30rpx;
}
.meta {
  display: flex;
  justify-content: space-between;
  font-size: 28rpx;
  color: #999;
}
</style>